﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺      
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 业务逻辑层工厂类代码  ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System.Reflection;
using Wedn.Net.BLL;
using Wedn.Net.Common;
using Wedn.Net.IBLL;

namespace Wedn.Net.BLLFactory
{
    /// <summary>
    /// 业务逻辑层工厂
    /// </summary>
    public class BLLFactory : IBLLFactory
    {
        private BLLFactory() { }
        private readonly string _path = Config.BLLPath;
        private readonly string _nspace = string.IsNullOrEmpty(Config.BLLNameSpace) ? Config.BLLPath : Config.BLLNameSpace;
        private ICommentManager _comment;
        /// <summary>
        /// CommentManager 实例
        /// </summary>
		public ICommentManager CommentManager
        {
            get
			{
				return _comment ?? (_comment = new CommentManager());
				//string className = nspace + ".CommentManager";
				//return CreateInstance<ICommentManager>(iComment, className);
			}
        }

        private ILinkManager _link;
        /// <summary>
        /// LinkManager 实例
        /// </summary>
		public ILinkManager LinkManager
        {
            get
			{
				return _link ?? (_link = new LinkManager());
				//string className = nspace + ".LinkManager";
				//return CreateInstance<ILinkManager>(iLink, className);
			}
        }

        private IOptionManager _option;
        /// <summary>
        /// OptionManager 实例
        /// </summary>
		public IOptionManager OptionManager
        {
            get
			{
				return _option ?? (_option = new OptionManager());
				//string className = nspace + ".OptionManager";
				//return CreateInstance<IOptionManager>(iOption, className);
			}
        }

        private IPostManager _post;
        /// <summary>
        /// PostManager 实例
        /// </summary>
		public IPostManager PostManager
        {
            get
			{
				return _post ?? (_post = new PostManager());
				//string className = nspace + ".PostManager";
				//return CreateInstance<IPostManager>(iPost, className);
			}
        }

        private IPostMetaManager _postmeta;
        /// <summary>
        /// PostMetaManager 实例
        /// </summary>
		public IPostMetaManager PostMetaManager
        {
            get
			{
				return _postmeta ?? (_postmeta = new PostMetaManager());
				//string className = nspace + ".PostMetaManager";
				//return CreateInstance<IPostMetaManager>(iPostMeta, className);
			}
        }

        private IRelationManager _relation;
        /// <summary>
        /// RelationManager 实例
        /// </summary>
		public IRelationManager RelationManager
        {
            get
			{
				return _relation ?? (_relation = new RelationManager());
				//string className = nspace + ".RelationManager";
				//return CreateInstance<IRelationManager>(iRelation, className);
			}
        }

        private ITermManager _term;
        /// <summary>
        /// TermManager 实例
        /// </summary>
		public ITermManager TermManager
        {
            get
			{
				return _term ?? (_term = new TermManager());
				//string className = nspace + ".TermManager";
				//return CreateInstance<ITermManager>(iTerm, className);
			}
        }

        private IUserManager _user;
        /// <summary>
        /// UserManager 实例
        /// </summary>
		public IUserManager UserManager
        {
            get
			{
				return _user ?? (_user = new UserManager());
				//string className = nspace + ".UserManager";
				//return CreateInstance<IUserManager>(iUser, className);
			}
        }

        private IUserMetaManager _usermeta;
        /// <summary>
        /// UserMetaManager 实例
        /// </summary>
		public IUserMetaManager UserMetaManager
        {
            get
			{
				return _usermeta ?? (_usermeta = new UserMetaManager());
				//string className = nspace + ".UserMetaManager";
				//return CreateInstance<IUserMetaManager>(iUserMeta, className);
			}
        }

		/// <summary>
        /// 实例化, 如果存在则返回对象, 不存在则返回null;
        /// </summary>
        /// <returns>泛型对象</returns>
        private T CreateInstance<T>(T instance, string className) where T : class
        {
            if (instance == null)
            {
                lock ("DbSessionLockHelper")
                {
                    instance = Assembly.Load(_path).CreateInstance(className) as T;
                }
            }
            return instance;
        }

        /// <summary>
        /// 保存数据库的改变状态
        /// </summary>
        /// <returns>受影响行数</returns>
        public int DbSaveChanges()
        {
            return DALFactory.DbSession.Instance.SaveChanges();
        }
		
        private static BLLFactory _factory;
        /// <summary>
        /// BLLFactory实例；
        /// </summary>
        public static BLLFactory Instance
        {
            get
            {
                return _factory ?? (_factory = new BLLFactory());
            }
        }
    }
} 
